package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.entity.Setmeal;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorsMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {
    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private DishFlavorsMapper dishFlavorsMapper;

    @Autowired
    private SetmealMapper setmealMapper;




    /**
     * 新增菜品
     *
     * @param dishDTO
     */
    @Transactional
    @Override
    public void save(DishDTO dishDTO) {

        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.save(dish);
        Long id = dish.getId();
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
            dishFlavorsMapper.insertBatch(flavors);
        }
    }

    /**
     * 菜品分页查询
     *
     * @param dishPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据菜品ids来删除菜品
     *
     * @param ids
     */

    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {
        //判断删除菜品时,状态是否为停售时才能删除
        for (Long id : ids) {
            Dish byIds = dishMapper.getById(id);
            if (byIds.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
        }
        //判断删除菜品时,套餐是否包含菜品

        List<Long> setmealIds = setmealMapper.getByDishId(ids);
        if (setmealIds != null && setmealIds.size() != 0) {
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }
        //删除菜品
//        for (Long id : ids) {
//            dishMapper.deleteById(id);
//            //删除菜品风味包含菜品id
//            dishFlavorsMapper.deleteByDishId(id);
//
//        }
        //删除菜品
        dishMapper.deleteByIds(ids);
        //删除菜品风味包含菜品id
        dishFlavorsMapper.deleteByDishIds(ids);


    }


    /**
     * 根据id查询对应的菜品和口味
     * @param id
     * @return
     */

    @Override
    public DishVO getByIdwithFlavor(Long id) {
        Dish dish = dishMapper.getById(id);
        DishVO dishVO = new DishVO();
        BeanUtils.copyProperties(dish, dishVO);
        List<DishFlavor> dishFlavor = dishFlavorsMapper.getByDishId(id);
        dishVO.setFlavors(dishFlavor);
        return dishVO;
    }


    /**
     * 修改菜品信息
     *
     * @param dishDTO
     * @return
     */
    @Override
    @Transactional
    public void update(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish);
        dishMapper.update(dish);
        Long id = dish.getId();
        //删除dishflavor表中dishId的信息
        dishFlavorsMapper.deleteByDishId(dish.getId());
        List<DishFlavor> flavors = dishDTO.getFlavors();
        if (flavors != null && flavors.size() != 0) {
            flavors.forEach(flavor -> {
                flavor.setDishId(id);
            });
            //添加dishflavor表中新增的信息
            dishFlavorsMapper.insertBatch(dishDTO.getFlavors());
        }
    }

    /**
     * 修改菜品状态信息
     *
     * @param status
     */
    @Override
    public void startorNot(Integer status, Long id) {
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
        // 如果是停售操作，还需要将包含当前菜品的套餐也停售
        if (status == StatusConstant.DISABLE) {
            List<Long> dishIds = new ArrayList<>();
            dishIds.add(id);
            List<Long> setMealDishIds = setmealMapper.getByDishId(dishIds);
            System.out.println(setMealDishIds);
            if (setMealDishIds != null && setMealDishIds.size() > 0){
                for (Long setMealDishId : setMealDishIds) {
                    Setmeal setmeal = new Setmeal();
                    setmeal.setId(setMealDishId);
                    setmeal.setStatus(status);
                    setmealMapper.update(setmeal);
                }

            }
        }
    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.list(dish);

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishFlavorsMapper.getByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }



}
